var addPerson;
var addItem;
var xmlhttp = new getXMLObject();

function softForm() {
	addSoft =  window.open("soft_form.html", "addSoft", "status=no, menubar=no, resizable=no, height=200, width=400");
}

function bookForm() {
	addBook = window.open("book_form.html", "addBook", "status=0, menubar=1, resizable=0, height=200, width=400");
}

function hardForm() {
	addHard = window.open("hard_form.html", "addHard", "height=200, width=400, status=0, menubar=1, resizable=0");
}

function peopleForm() {
	addPerson = window.open("people_form.html", "addPerson", "fullscreen=0, menubar=1, status=0, height=200, width=400");
}

function queryPeople() {
	queryPeople = window.open("query_people.html", "addPerson", "status=0, menubar=1, resizable=0, height=100, width=400");
}

function getXMLObject() {
    var xmlHttp = false;
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
    }
    catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
        }
        catch (e2) {
            xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
        }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}
 
function ajaxFunction(url) //url passed from html page including parameters
{
  if(xmlhttp) 
  {
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = handleServerResponse;
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send();
	
  }
}
 
function handleServerResponse() 
{
   if (xmlhttp.readyState == 4) 
   {
     if(xmlhttp.status == 200) 
	 {
       document.getElementById("records_container").innerHTML=xmlhttp.responseText;
     }
     else 
	 {
        alert("Error during AJAX call. Please try again");
     }
   }
}

function printer()
{
	var head = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"printFormat.css\"><title>Print Preview</title></head>";
	var bodyOpen =  "<body>";
	var content1 = document.getElementById('printSection').innerHTML;
	var bodyClose = "<p>Here is a preview of your printed page</p></body></html>";
	
	var pageCode = head + bodyOpen + content1 + bodyClose;
	printWin = window.open('','printwin','left=200,top=200,width=550,height=400,scrollbars=1,menubar=1');
	printWin.document.write(pageCode); 
	printWin.print();
	printWin.close();
}

function queryPeople() {
    var add1 = "<table class=\"queryCont\"><tr><td>Name</td><td><input type=\"text\" name=\"name\"/>";
	var add2 = "<input type=\"button\" onclick=\"queryFunction()\" value=\"Submit\"/></td></tr></table>";
	
	document.getElementById("querySection").innerHTML = add1+add2;
}

function queryFunction() {
    if(xmlhttp) 
    {
		var params = "function=query&table=people&name=" + document.getElementsByName("name")[0].value;
		var url = "http://localhost:8080/HI%20Resource%20Catalog/servlet/editItemInDB?" + params;
  		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = handleQueryResponse;
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send();
		
	}
}

function handleQueryResponse() 
{
   if (xmlhttp.readyState == 4) 
   {
     if(xmlhttp.status == 200) 
	 {
       document.getElementById("querySection").innerHTML="";
	   document.getElementById("records_container").innerHTML=xmlhttp.responseText;
     }
     else 
	 {
        alert("Error during AJAX call. Please try again");
     }
   }
}